<?php
$this->pageTitle = 'Post a new logo - Administrator';
?>

<script type="text/javascript">
    $(document).ready(function(){
        function startUpload(){
            $('#admin-upload-logo-loading').empty().append('uploading ... ').show();
            $('#admin-upload-logo').hide();
            $('#finishUpload').empty();
        }

        function stopUpload(){
            $('#admin-upload-logo-loading').fadeOut('slow');
            $('#admin-upload-logo').show();
        }

        $('#PostUploadForm').bind("submit",function(){
            startUpload();
            var iframe = document.createElement("DIV");
            $('#upload_target').load(function() {
                stopUpload();
                var iframe = $('#upload_target');

                /** Get a response from the server in plain text */
                var myFrame = document.getElementById(iframe.attr('name'));
                var data = eval("(" + $(myFrame.contentWindow.document.body).text() + ")");

                if(data.result == '1'){
                    $('#admin-upload-logo').hide().empty();
                    $('#admin-upload-content').show('slow');
                    $('#admin-upload-image').append('<img src="<?=$baseUrl.'/webroot'?>'+data.thumb +'" />');
                    $('[id=PostCreated]').val(data.created);
                    $('[id=PostPostId]').val(data.logo);
                    $('[id=PostCatalog').val(data.catalog);
                }else if (data.error) {
                   $('#admin-upload-logo').show('slow');
                    $('#admin-upload-logo-loading').fadeOut('slow');
                    var warning = data.error;
                    if (warning == 4) {	$('#finishUpload').fadeIn('slow').append('Nothing logo uploaded');}
                    else if (warning == 1) {	$('#finishUpload').fadeIn('slow').append('Logo size overload due to limit capacity server upload.');	}
                    else if (warning == 2) {	$('#finishUpload').fadeIn('slow').append('Logo size overload due to form capacity form.');	}
                    else if (warning == 3) {	$('#finishUpload').fadeIn('slow').append('Logo not complete uploaded. Try again.');	}
                    else if (warning == 5) {	$('#finishUpload').fadeIn('slow').append('Temporary folder is missing');	}
                    else if (warning == 6) {	$('#finishUpload').fadeIn('slow').append('Temporary folder cannot accessed. Check permission');	}
                    else if (warning == 7) {	$('#finishUpload').fadeIn('slow').append('Only jpeg,jpg,gif dan png permitted.');	}
                    else if (warning == 8) {	$('#finishUpload').fadeIn('slow').append('Failed when moving logo to server folder');	}
                    else if (warning == 9) {	$('#finishUpload').fadeIn('slow').append('Failed creating logo thumbnail.');	}
                    else if (warning == 10) {  $('#finishUpload').fadeIn('slow').append('Upload attack detected!');	}
                    else if (warning == 11) {	$('#finishUpload').fadeIn('slow').append('Logos failed saving in database.');	}
                    else if (warning == 13) {	$('#finishUpload').fadeIn('slow').append('Logos does not having match resolution');	}
                    else {	$('#finishUpload').fadeIn('slow').append('Failed saving logo. <a href="<?=$baseUrl?>/posts/manage">Please active your logo here</a> ');	}
                } else {
                    $('#finishUpload').empty().append('Your session expired. Please <?=$html->link('login','/login')?> again').fadeIn('slow');
                }
            });
        });

        $('#upload-category-submit').click(function(){
            $('#upload-category-loading').empty().append('adding category....');
            var input_category = $('[id=Categories]').val();
            if(input_category.length<1){$("#upload-category-loading").empty().append('Category still empty');}
            if(input_category.length>0){
                $.ajax({
                    type: "POST",
                    data: ({category : input_category}),
                    dataType: "json",
                    url: "<?=$baseUrl?>/categories/add",

                    success: function(data){
                        if(data.result){
                            $('#upload-category-loading').empty();
                            categoryList();
                        }else if(data.error){
                            if(data.error == '0'){ $('#upload-category-loading').empty().append('Category still empty');}
                            else{$('#upload-category-loading').empty().append('Category is exist'); }
                        }else{
                            $('#upload-category-loading').empty().append('Your session expired. Please <?=$html->link('login','/login')?> again');
                        }
                    },
                    error:function(XMLHttpRequest,textStatus, errorThrown)
                    {
                        var status = XMLHttpRequest.status;
                        if(status == '403'){
                            $('#upload-category-loading').empty().append('Your session expired. Please <?=$html->link('login','/login')?> again');
                        }
                    }
                });
        }

        return false;
        });

       function categoryList(){
            $('#upload-category-form').hide();
            $("#upload-category-loading").empty().append("fetching categories .....");
            $.getJSON('<?=$baseUrl?>/categories/category', function(data) {
                if(data.error){
                    $('#upload-category-form').show();
                    $("#upload-category-loading").empty().append('Connection error ... ');
                }else if(data.result){
                    $('#upload-category-form').empty().append('<select id="PostCategory" title="Category" name="data[Post][Category]">');
                    $.each(data.category,function(i,item) {
                        $('select#PostCategory').append('<option value="'+item.Category.category_id+'">'+item.Category.name +'</option>');
                    });
                    $("#upload-category-form").append('</select>').fadeIn();
                    $("#upload-category-loading").empty().append('New category added!');
                }else{
                    $('#upload-category-form').show();
                    $("#upload-category-loading").empty().append('Your session expired. Please <?=$html->link('login','/login')?> again ');
                }
            });
        }

        $('#upload-tag-submit').click(function(){
           $('#upload-tag-loading').empty().append('adding tags....');

           var post_id = $('[id=PostPostId]').val();
           var input_tags = $('[id=TagName]').val();
           
           if(input_tags.length<1){$("#upload-tag-loading").empty().append('Tags still empty');}

           if(input_tags.length>0){
                $.ajax({
                    type: "POST",
                    data: ({tags : input_tags,id: post_id}),
                    dataType: "json",
                    url: "<?=$baseUrl?>/posts/addTag",

                    success: function(data){
                        if(data.result){
                            $('#upload-tag-loading').empty();
                            $('#upload-tag-result').append(' '+data.tags);
                            $('#TagName').val('');
                        }else if(data.error){
                            if(data.error == '0'){ $('#upload-tag-loading').empty().append('Tag still empty');}
                            else if(data.error == '1') {$('#upload-tag-loading').empty().append('Post id is missing, please upload the logo first'); }
                            else {
                                $('#upload-tag-loading').empty().append('Your session expired. Please <?=$html->link('login','/login')?> again');
                            }
                        }else{
                            $('#upload-tag-loading').empty().append('Your session expired. Please <?=$html->link('login','/login')?> again');
                        }
                    },
                    error:function(XMLHttpRequest,textStatus, errorThrown)
                    {
                        var status = XMLHttpRequest.status;
                        if(status == '403'){
                            $('#upload-tag-loading').empty().append('Your session expired. Please <?=$html->link('login','/login')?> again');
                        }
                    }
                });
            }

           return false;
        });

        $('#upload-publish-submit').click(function(){
            $('#upload-publish-result').empty().append('Saving ....');
            $('#upload-left-content .error').empty();
            var input_title = $('#PostTitle').val();
            var input_description = $('#PostDescription').val();
            var post_id = $('[id=PostPostId]').val();
            var input_category = $('[id=PostCategory]').val();
            var input_meta = $('[id=PostMeta]').val();
            var input_keyword = $('[id=PostKeyword]').val();
            
            if(input_title.length<1){$('#titleError').empty().append('Title still empty.');}
            if(input_description.length<1){$('#descError').empty().append('Description still empty.');}
            if(post_id.length<1){$('#upload-publish-result').empty().append('Please upload logo first.');}

            if(input_title.length>0 && input_description.length>0 && post_id.length>0){
                 $.ajax({
                    type: "POST",
                    data: ({title : input_title,id: post_id,description:input_description,category:input_category,meta:input_meta,keyword:input_keyword}),
                    dataType: "json",
                    url: "<?=$baseUrl?>/posts/savePost",

                    success: function(data){
                        $('#upload-publish-result').empty();
                        if(data.result){
                            $('#admin-upload-report').empty().append('<h1>Save post succesfully! <a href="<?=$baseUrl?>/view/'+data.result+'">view the post</a></h1>');
                        }else if(data.error){
                            if(data.error == '0'){ $('#titleError').empty().append('Title still empty.');}
                            else if(data.error == '1') {$('#descError').empty().append('Description still empty.');}
                            else if(data.error == '2') {$('#upload-publish-result').empty().append('Please upload logo first.');}
                            else {
                                $('#upload-publish-result').empty().append('Your session expired. Please <?=$html->link('login','/login')?> again');
                            }
                        }else{
                            $('#upload-publish-result').empty().append('Your session expired. Please <?=$html->link('login','/login')?> again');
                        }
                    },
                    error:function(XMLHttpRequest,textStatus, errorThrown)
                    {
                        var status = XMLHttpRequest.status;
                        if(status == '403'){
                            $('#upload-publish-result').empty().append('Your session expired. Please <?=$html->link('login','/login')?> again');
                        }
                    }
                });
            }else{
                $('#admin-upload-report').empty().append('<h1>Failed. Some form still empty.</h1>');
            }

            return false;
        });
        
        $('#upload-draft-submit').click(function(){
            $('#upload-publish-result').empty().append('Saving ....');
            $('#upload-left-content .error').empty();
            var input_title = $('#PostTitle').val();
            var input_description = $('#PostDescription').val();
            var post_id = $('[id=PostPostId]').val();
            var input_category = $('[id=PostCategory]').val();
            var input_meta = $('[id=PostMeta]').val();
            var input_keyword = $('[id=PostKeyword]').val();
            
            if(input_title.length<1){$('#titleError').empty().append('Title still empty.');}
            if(input_description.length<1){$('#descError').empty().append('Description still empty.');}
            if(post_id.length<1){$('#upload-publish-result').empty().append('Please upload logo first.');}

            if(input_title.length>0 && input_description.length>0 && post_id.length>0){
                 $.ajax({
                    type: "POST",
                    data: ({title : input_title,id: post_id,description:input_description,category:input_category,meta:input_meta,keyword:input_keyword}),
                    dataType: "json",
                    url: "<?=$baseUrl?>/posts/saveDraft",

                    success: function(data){
                        $('#upload-publish-result').empty();
                        if(data.result){
                            $('#admin-upload-report').empty().append('<h1>Save as draft succesfully! <a href="<?=$baseUrl?>/view/'+data.result+'">view the post</a></h1>');
                        }else if(data.error){
                            if(data.error == '0'){ $('#titleError').empty().append('Title still empty.');}
                            else if(data.error == '1') {$('#descError').empty().append('Description still empty.');}
                            else if(data.error == '2') {$('#upload-publish-result').empty().append('Please upload logo first.');}
                            else {
                                $('#upload-publish-result').empty().append('Your session expired. Please <?=$html->link('login','/login')?> again');
                            }
                        }else{
                            $('#upload-publish-result').empty().append('Your session expired. Please <?=$html->link('login','/login')?> again');
                        }
                    },
                    error:function(XMLHttpRequest,textStatus, errorThrown)
                    {
                        var status = XMLHttpRequest.status;
                        if(status == '403'){
                            $('#upload-publish-result').empty().append('Your session expired. Please <?=$html->link('login','/login')?> again');
                        }
                    }
                });
            }else{
                $('#admin-upload-report').empty().append('<h1>Failed. Some form still empty.</h1>');
            }

            return false;
        });

    });
</script>

<div id="admin-wrapper">
    <div id="admin-upload">
        <div id="admin-upload-logo">
            <h2>Uploading Logo</h2>
            <?php
            e($form->create('Post', array('action' => 'upload','method'=>'post', 'type' => 'file', 'target' => 'upload_target')));
            e($form->file('filedata',array('class'=>'file','title'=>'Choice image or files form your computer')));
            e($form->submit('Upload', array (
                          'title'=> 'Upload image / files',
                          'id'=>'upload-submit',
                          'div'=>'uploadsubmit')));
            ?>
            <?php
            echo $form->end();
            ?>
            <div class="clear"></div>
        </div>
        <div id="admin-upload-logo-loading"></div>
        <iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0;"></iframe>
        <div id="finishUpload"></div>
        <div id="admin-upload-report"></div>
        <div id="admin-upload-content" style="display:none;">
            <div id="upload-left">
                <div id="upload-left-wrap">
                    <div class="admin-box-title">
                        <h2>Title & Description</h2>
                    </div>
                    <div id="upload-left-content">                        
                        <div class="desc"><strong>Title :</strong></div>
                        <div class="box" id="logoTitle"><?=$form->input('Post.title',array('label'=>false,'div'=>false,'title' => 'Logo title','size'=>'43','maxLength'=>'43'))?></div>
                        <div class="error" id="titleError"></div>
                        <div class="desc"><strong>Description : </strong></div>
                        <div class="text" id="logoDesc"><?=$form->textarea('Post.description',array('label'=>false,'div'=>false,'cols'=>'70','rows'=>'11'))?></div>
                        <div id="descError" class="error"></div>
                        <div class="desc"><strong>Meta Description :</strong></div>
                        <div class="box" id="logoTitle"><?=$form->input('Post.meta',array('label'=>false,'div'=>false,'title' => 'Logo title','size'=>'166','maxLength'=>'166'))?></div>
                        <br />
                        <div class="desc"><strong>Keyword :</strong></div>
                        <div class="box" id="logoTitle"><?=$form->input('Post.keyword',array('label'=>false,'div'=>false,'title' => 'Logo title','size'=>'50','maxLength'=>'60'))?></div>
                        <?=$form->hidden('Post.thumb');?>
                        <?=$form->hidden('Post.created');?>
                        <?=$form->hidden('Post.post_id');?>
                        <?=$form->hidden('Post.catalog');?>
              
                    </div>
                </div>
            </div>
            <div id="upload-right">
                <div class="admin-box-wrap">
                    <div class="admin-box-title">
                        <h2>Publish</h2>
                    </div>
                    <div class="admin-box-content" id="upload-publish">
                        <div class="desc">
                            <?=$form->submit('Publish', array (
                                      'title'=> 'Publish logo',
                                      'id'=>'upload-publish-submit',
                                      'div'=>'uploadpublishsubmit'))
                            ?>
                       </div>
                       <div class="desc">
                            <?=$form->submit('Save as Draft', array (
                                      'title'=> 'Save as Draft logo',
                                      'id'=>'upload-draft-submit',
                                      'div'=>'uploaddraftsubmit'))
                            ?>
                        </div>
                        <div class="clear"></div>
                        <div id="upload-publish-result"></div>
                    </div>
                </div>

                <div class="admin-box-wrap">
                    <div class="admin-box-title">
                        <h2>Tags</h2>
                    </div>
                    <div class="admin-box-content" id="upload-tag">
                        <div class="desc"><i>Use comma to separate tags</i></div>
                        <div class="box" id="logoTag"><?=$form->input('Tag.name',array('label'=>false,'div'=>false,'title' => 'Tags','size'=>'30'))?></div>
                        <div class="desc">
                            <?=$form->submit('Add', array (
                                      'title'=> 'Adding tags',
                                      'id'=>'upload-tag-submit',
                                      'div'=>'uploadtagsubmit'))
                            ?>
                        </div>
                        <div class="clear"></div>
                        <div id="upload-tag-loading"></div>
                        <div id="upload-tag-result"></div>
                    </div>
                </div>

               <div class="admin-box-wrap">
                    <div class="admin-box-title">
                        <h2>Categories</h2>
                    </div>
                    <div class="admin-box-content" id="upload-category">
                        <div id="upload-category-form">
                            <?=$form->input('Post.Category',array('label'=>false,'div'=>false,'title'=>'Category'))?>                        
                        </div>
                        <div class="desc"><i>Add new category</i></div>
                        <div class="box" id="logoCategory"><?=$form->input('Categories',array('label'=>false,'div'=>false,'title' => 'Tags','size'=>'30'))?></div>
                        <div class="category-submit">
                            <?=$form->submit('Add', array (
                                      'title'=> 'Adding a new category',
                                      'id'=>'upload-category-submit',
                                      'div'=>'uploadcategorysubmit'))
                            ?>
                        </div>
                        <div class="clear"></div>
                        <div id="upload-category-loading"></div>
                        <div id="upload-category-result"></div>
                    </div>
                </div>

                <div class="admin-box-wrap">
                    <div class="admin-box-title">
                        <h2>Logo image</h2>
                    </div>
                    <div class="admin-box-content" id="upload-category">
                        <div id="admin-upload-image"></div>
                    </div>
                </div>
            </div>
            <div class="clear"></div>
        </div>
    </div>
</div>
